#include "diskchecker.h"
#include "diskmanager.h"
#include "msgbox.h"
#include "systempreset.h"
#include "userlogger.h"

DiskChecker::DiskChecker()
{
}

DiskChecker::DiskUsage DiskChecker::checkFreeSpace(const QString &driver)
{
    QString path = driver;
    if (!DiskManager::dirExists(driver))
    {// 驱动器不存在，检查当前程序所在分区
        path = qApp->applicationDirPath();
    }

    quint64 minSize = SystemPreset::instance()->getStringData(EnumPresetItem::DiskMinSize).toULongLong();
    quint64 warningSize = SystemPreset::instance()->getStringData(EnumPresetItem::DiskWarningSize).toULongLong();
    quint64 freeSpaceSize = DiskManager::getFreeSpace(path);
    if (freeSpaceSize < minSize)
    {// 磁盘可用空间耗尽，不允许实验
        QString message = QObject::tr("Running out of disk space, Please contact the manufacturer");
        MsgBox::information(0, QObject::tr("Error"), message);
        UserLogger::log(message);
        return DiskChecker::Exhaust;
    }

    if (freeSpaceSize < warningSize)
    {// 磁盘可用空间不足，弹窗提示，允许继续实验
        QString message = QObject::tr("Low disk space");
        MsgBox::information(0, QObject::tr("Warning"), message);
        UserLogger::log(message);
        return DiskChecker::Warning;
    }

    return DiskChecker::Enough;
}
